import CreepController from "../controller"


class RoleWallFiller {

    /**
     *
     * @param creep
     * @returns
     */
    static loop(creep: Creep) {
        // 有焦点墙就优先刷
        if (creep.memory.fillWallId) {
            creep.steadyWall()
        }
        // 否则就按原计划维修
        else {
            const filling = creep.fillDefenseStructure()
            if (!filling) {

            }
        }

        if (creep.store.getUsedCapacity() === 0) {
            if (creep.store.getFreeCapacity(RESOURCE_ENERGY) > 0 && creep.ticksToLive > 50) {
                const storeObj = CreepController.findStorage(creep, 500)
                if (storeObj) {
                    const status = creep.withdraw(storeObj, RESOURCE_ENERGY)
                    if (status == ERR_NOT_IN_RANGE) {
                        creep.goTo(storeObj.pos)
                    }
                }
            }
            else {
                const storeObj = creep.room[STRUCTURE_STORAGE]
                if (storeObj) {
                    const status = creep.transfer(storeObj, RESOURCE_ENERGY)
                    if (status == ERR_NOT_IN_RANGE) {
                        creep.goTo(storeObj.pos)
                    }
                }
            }
        }
    }
}

export default RoleWallFiller
